
<template>
    <div class="map" ref="map">
        <canvas v-show="flag" id="rJBGRFoRsasBsmhxpoxqMGhrlnzEwAPF" :width="cWidth" :height="cHeight" class="mapbox"
            @click="canvastap"></canvas>
        <div v-show="!flag" class="loadbox">
            <van-loading type="spinner" color="#666" vertical >地图加载中...</van-loading>
        </div>
    </div>
</template>
<script>
import uCharts from '@qiun/ucharts';
var uChartsInstance = {};
function getH5Offset(e) {
    e.mp = {
        changedTouches: []
    };
    e.mp.changedTouches.push({
        x: e.offsetX,
        y: e.offsetY
    });
    return e;
}
export default {
    props:{
        mapdata:Array,
        type:[Number,String],
        colors:{
            type:Array,
            default(){
                return  ["#a21313", "#b71c1c", "#be2222", "#c62828","#cb2929","#d32f2f","#dd312d","#f44336","#eb4643","#ef5350","#e75c5c","#ef5350","#e57373","#e68383","#ef9a9a","ffcdd2","#ffebee"]
            }
        }
        
    },   // type = 1 省地图  type =2 城市地图 
    data() {
        return {
            school_info:[], //区域排名
            openid: $.cookie("openid"),
            flag:false,
            cWidth: 375,
            cHeight: 300,
            chartData: {},
            //您可以通过修改 config-ucharts.js 文件中下标为 ['map'] 的节点来配置全局默认参数，如都是默认参数，此处可以不传 opts 。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式，达到页面简洁的需求。
            opts: {
                // 这里就是地图每个城市的颜色，
                // color: ["#a20a23",  "#d51838" , "#e91a3c",  "#fc2e52",  "#ff5d78",  "#ff98a9","#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4", "#ea7ccc"],
                // color: ["#a21313", "#b71c1c", "#be2222", "#c62828","#cb2929","#d32f2f","#dd312d","#f44336","#eb4643","#ef5350","#e75c5c","#ef5350","#e57373","#e68383","#ef9a9a","ffcdd2","#ffebee"],
                padding: [0, 0, 0, 0],
                dataLabel: true, //是否显示图表区域内数据点上方得数据文案
                enableScroll: false, //图表拖拽
                fontSize: 12,//全局字体大小
                extra: {
                    map: {//地图
                        border: true, //是否绘制各类别中间的分割线
                        borderWidth: 1, //分割线的宽度
                        borderColor: "#666666", //分割线的颜色
                        fillOpacity: 0.6, //区域内填充透明度
                        activeBorderColor: "#F04864", //点击激活时分割线的颜色
                        activeFillColor: "#FACC14",//点击激活时分区域内填充颜色
                        activeFillOpacity: 1, // 点击激活时分区域内填充颜色透明度
                    },
                    column: {
                        type: "group",//柱状图类型
                        width: 30,
                        activeBgColor: "#000000", //当前点击的图的背景颜色
                        activeBgOpacity: 0.08 //当前点击图的背景颜色的透明度
                    }
                }
            },
        }
    },
    methods: {
        changepx(){
            // const width = document.body.clientWidth || document.documentElement.clientWidth
            const width = this.$refs.map.clientWidth;
            console.log(width);
            this.cWidth = width;
            this.cHeight = width;
        },  
        getServerData() {
            //模拟从服务器获取数据时的延时
            setTimeout(() => {
                //模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
                // let res = {series:{
                //     说明: "地图数据遵循geoJson地图数据交换格式，引用方式请见插件市场示例项目：https://ext.dcloud.net.cn/plugin?id=271",
                //     备注: "其他地图数据下载地址：http://datav.aliyun.com/tools/atlas/",
                //     注意: "如果您引用数据的坐标系是WGS84地球坐标系，会导致图形变形，需要在【扩展配置】中开启【是否进行WGS84转墨卡托投影】功能（开启后因大量的数据运算tooltip可能会不跟手，建议自行转换为墨卡托坐标系，可参照源码内function lonlat2mercator()）"
                //     }};

                let res = {
                    series: this.mapdata
                }
                // this.chartsDataMaps.series = mapdata.features
                // this.chartData = JSON.parse(JSON.stringify(res));
                this.drawCharts('rJBGRFoRsasBsmhxpoxqMGhrlnzEwAPF', res);
                this.$nextTick(()=>{
                    this.flag = true;
                })
            }, 500);
        },
        drawCharts(id, data) {
            const ctx = document.getElementById(id).getContext("2d");
            this.$set(this.opts,'color',this.colors);
            uChartsInstance[id] = new uCharts({
                type: "map",
                context: ctx,
                series: data.series,
                width: this.cWidth,
                height: this.cHeight,
                ...this.opts,
            });
        },
        canvastap(e) {
            uChartsInstance[e.target.id].touchLegend(getH5Offset(e));
            uChartsInstance[e.target.id].showToolTip(getH5Offset(e));
            var index = uChartsInstance[e.target.id].getCurrentDataIndex(e);
            console.log(index);
            if(index!=-1){
                if(this.type=='1'){
                    this.$router.push({ name: "citymap" ,params:{cityIndex:index}});
                }else{
                    console.log("城市数据信息是 ");
                    console.log(this.mapdata[index]);
                    console.log('当前城市的 adcode 码 = ' + this.mapdata[index].properties.adcode);
                    
                    var code = this.mapdata[index].properties.adcode;
                    this.$emit('getcurrentcode',code);
                };
            };
        },
    },
    mounted(){
        this.$nextTick(()=>{
            this.changepx();
            this.getServerData();
        }); 
    }
}
</script>

<style lang="scss" scoped>
.map{
    box-sizing: border-box;
    border:1px solid #ddd;
    border-radius: 10px;
    background: #fff;
    width:100%;
    min-height:300px;
    display:flex;
    align-items: center;
    .loadbox{
        width:100%;
        height:100%;
        display: flex;
        justify-content: center;
        align-items: center;
    }
}
</style>